$(function () {
    $.ajax({
        type: 'POST',
        async: false,
        url: '/zjj/queryAllProductionSummaries.action',
        contentType: 'application/json;charset=utf-8',
        success: function (result) {
            $.each(result.data, function (i, v) {
                let tr = $(`<tr>
                    <td>${v.manufactureId}</td>
                    <td>${v.productId}</td>
                    <td>${v.productName}</td>
                    <td>${v.amount}</td>
                    <td>${(v.testedAmount == undefined ? '生产中' : v.testedAmount)}</td>
                    <td>${(v.checkTag == 'S001-0' ? '等待审核' : v.checkTag == 'S001-1' ? '审核通过' : '审核不通过')}</td>
                    <td>${(v.manufactureProcedureTag == 'S002-0' ? '等待' : v.manufactureProcedureTag == 'S002-1' ? '执行' : '完成')}</td>
                    <td><button value='${v.manufactureId}' data-toggle='modal' data-target='#myModal' class='btn btn-info' onclick='examine(this)'>查看</button></td>
                           </tr>`)
                $("#tbodyCommodity").append(tr)
            })
            jQuery.getScript("../js/datatables.init.js")
        }
    })
})
let arrList;
let dataView;
examine = e => {
    $.ajax({
        type: 'POST',
        async: false,
        url: '/zjj/queryPendingProductionDispatchOrders.action',
        contentType: 'application/json;charset=utf-8',
        data: $(e).val(),
        success: function (result) {
            $("#tbody").html('')
            arrList = result.data;
            $("#workOrderNumber1").text($(e).parent().parent().children().eq(0).text())
            $("#approvalStatus").text($(e).parent().parent().children().eq(5).text())
            $("#manufacturingState").text($(e).parent().parent().children().eq(6).text())
            $("#workOrderMaker").val(arrList.designer)
            $("#productNumber").val(arrList.productId)
            $("#productName").val(arrList.productName)
            $("#productionQuantity").val(arrList.amount)
            $("#qualifiedQuantity").val(arrList.testedAmount == undefined ? '生产中' : arrList.testedAmount)
            $("#totalCostOfDesignManHours").val(arrList.labourCostPriceSum)
            $("#totalDesignMaterialCost").val(arrList.moduleCostPriceSum)
            $("#actualTotalCostOfLabor").val(arrList.realLabourCostPriceSum == undefined ? '0' : arrList.realLabourCostPriceSum)
            $("#actualTotalMaterialCost").val(arrList.realModuleCostPriceSum == undefined ? '0' : arrList.realModuleCostPriceSum)
            $("#registerPerson").val(arrList.register)
            $("#checkInTime").val(arrList.registerTime)
            $("#reviewer").val(arrList.checker)
            $("#reviewTime").val(arrList.checkTime)
            $("#auditOpinion").val(arrList.remark)
            $.each(arrList.productionProcesses, function (i, v) {
                let tr = $(`<tr>
                    <td>${i + 1}</td>
                    <td>${v.procedureFinishTag == 'G004-0' ? '等待' : v.procedureFinishTag == 'G004-3' ? '完成' : '审核中'}</td>
                    <td>${v.procedureName}</td>
                    <td>${v.subtotal}</td>
                    <td>${v.realSubtotal == undefined ? '0' : v.realSubtotal}</td>
                    <td>${v.moduleSubtotal}</td>
                    <td>${v.realModuleSubtotal == undefined ? '0' : v.realModuleSubtotal}</td>
                    <td><button value='${v.procedureName}' data-toggle='modal' data-target='#myModal1' class='btn btn-info' onclick='viewHours(this)'>查看工时</button>
                    <button value='${v.procedureName}' data-toggle='modal' data-target='#myModal2' class='btn btn-info' onclick='viewMaterial(this)'>查看物料</button>
                    </td>
                        </tr>`)
                $("#tbody").append(tr)
            })

        }
    })
    $.ajax({
        type: 'POST',
        async: false,
        url: '/zjj/queryAllIngProductionProcessRecords.action',
        contentType: 'application/json;charset=utf-8',
        data: $(e).val(),
        success: function (result) {
            dataView = result.data;
        }
    })

}
viewHours = e => {
    $("#tbBody").html('')
    $.each(dataView.productionProcesses, function (i, v) {
        if (v.procedureName == $(e).val()) {
            $("#workOrderNumber").val($("#workOrderNumber1").text())
            $("#processName").val(v.procedureName)
            $("#actualHoursWorked").val(v.labourHourAmount)
            $("#actualLaborCost").val(v.subtotal)
            let tr = $(`<tr>
            <td>${v.detailsNumber}</td>
            <td>${v.procedureResponsiblePerson}</td>
            <td>${v.labourHourAmount}</td>
            <td>${v.costPrice}</td>
            <td>${v.subtotal}</td>
            <td>${v.register}</td>
            <td>${v.registerTime}</td>
            </tr>`)
            $("#tbBody").append(tr)
        }
    })
    $("#myModal1").modal('show');
}

viewMaterial = e => {
    $("#tbBody1").html('')
    let actualMaterialCost = 0;
    $.each(dataView.productionProcesses, function (i, v) {
        if (v.procedureName == $(e).val()) {
            $.each(v.moduleIng, function (c, f) {
                if (v.id == f.parentId) {

                    actualMaterialCost += f.subtotal;
                    $("#workOrderNumber2").val($("#workOrderNumber1").text())
                    $("#processName1").val(v.procedureName)
                    let tr = $(`<tr>
            <td>${c + 1}</td>
            <td>${f.productName}</td>
            <td>${f.detailsNumber}</td>
            <td>${f.amount}</td>
            <td>${f.subtotal}</td>
            <td>${f.costPrice}</td>
            <td>${v.register}</td>
            <td>${v.registerTime}</td>
            </tr>`)
                    $("#tbBody1").append(tr)
                }
            })
        }
    })
    $("#actualMaterialCost").val(actualMaterialCost)
}